//获取当前年月
function getNowYearMonthDay() {
  let obj = {}
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  obj.year = year
  obj.month = month
  obj.day = day
  return obj
}

//根据年月获取几天
function getNowTheDays(year, month) {
  const date = new Date(year, month, 0)
  const day = date.getDate()
  return day
}

//计算n天的时间戳是多少
function getNDayOfTimeStamp(n) {
  return n * (60 * 60 * 24)
}

//根据时间戳获取当前是第几天或第几月
function getNowDayOrMonth(timestamp, type) {
  let date = new Date()
  date.setTime(timestamp * 1000)
  // let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? "0" + m : m
  let d = date.getDate()
  d = d < 10 ? "0" + d : d
  if (type == "year") {
    return m
  } else {
    return d
  }
}

//判断是否闰年
function isLeapYear(year) {
  let day = 0
  if (year % 4 == 0 && year % 100 != 0) {
    //是闰年
    day = 29
  } else {
    day = 28
  }
  return day
}

//将时间字符串'2022-07-22 13:45:44'转为时间戳
function getTimestampFromString(str) {
  let timestamp = new Date(str).getTime()
  return timestamp
}

//将时间戳转为 年月日
function changeToYearMonthDay(timestamp, type) {
  let date = new Date(timestamp * 1000)
  let Y = date.getFullYear()
  let M =
    date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1
  let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + ""
  let h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ""
  let m =
    (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ""
  let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
  if (type == "month") {
    return Y + "-" + M + "-" + D
  } else if (type == "year") {
    return Y + "-" + M
  } else if (type == "all") {
    return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s
  }
}

//获取当前时间的时间戳
function getNowTimeStamp() {
  let dateStamp = String(new Date().valueOf()).substring(0, 10)
  return dateStamp
}

// 中国格式化时间转时分
function transformHourMinute(time) {
  if (!time) return ''
  let date = new Date(time)
  // let Y = date.getFullYear() + "-"
  // let M =
  // (date.getMonth() + 1 < 10
  //   ? "0" + (date.getMonth() + 1)
  //   : date.getMonth() + 1) + "-"
  // let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
  let h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":"
  let m =
    (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes())
  // let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
  return h + m
}

export {
  getNowYearMonthDay,
  getNowTheDays,
  getNDayOfTimeStamp,
  getNowDayOrMonth,
  isLeapYear,
  getTimestampFromString,
  changeToYearMonthDay,
  getNowTimeStamp,
  transformHourMinute
}
